package com.github.sbb.sherry.primitives.config.datasource;

import com.github.sbb.sherry.common.base.Preconditions;
import com.github.sbb.sherry.common.base.ValidateResult;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
@NoArgsConstructor
public class MongoDBDataSourceConfig implements DataSourceConfig {

  private String host;
  private int port = 27017;
  private String username;
  private String password;
  private String database;

  private int minSize;
  private int maxSize;
  private long maxWaitTimeMS;


  @Override
  public ValidateResult validate() {
    try {
      Preconditions.checkNotNull(host, "host 不能为空");
      Preconditions.checkRange(port, 1, 65535, "post 应该在 1 到 65535 之间");

      Preconditions.checkNotNull(username, "username 不能为空");
      Preconditions.checkNotNull(password, "password 不能为空");
      Preconditions.checkNotNull(database, "database 不能为空");
      Preconditions.checkArgument(maxSize > 0, "maxSize > 0");
      Preconditions.checkArgument(minSize >= 0, "minSize >= 0");
      Preconditions.checkArgument(maxSize >= minSize, "maxSize >= minSize");
    } catch (Exception e) {
      return new ValidateResult(false, e.getMessage());
    }
    return new ValidateResult(true, null);
  }
}
